package offer;

/**
 * @Author Elephas
 * @Date 2022/1/29
 **/
@FunctionalInterface
public interface SwapPairs {
    ListNode swapPairs(ListNode head);
}
class SwapPairsImpl1 implements SwapPairs{

    @Override
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0,null), current = head, tail = dummy, node1, node2;
        while(current != null){
            node1 = current;
            current = current.next;
            if(current == null){
                node1.next = tail.next;
                tail.next = node1;
                tail = node1;
            }else{
                node2 = current;
                current = current.next;
                // insert node2 to tail
                node2.next = tail.next;
                tail.next = node2;
                tail = node2;
                // insert node1 to tail
                node1.next = tail.next;
                tail.next = node1;
                tail = node1;
            }
        }
        tail.next =null;
        return dummy.next;
    }
}
